unit ufrmMdiChildPadrao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uDMConexao;

type
  TfrmMdiChildPadrao = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    fOnRetorno: TFuncaoRetorno;
    procedure SetOnRetorno(const Value: TFuncaoRetorno);
    function GetCallerControl: TControl;
    procedure SetCallerControl(const Value: TControl);
  public
    { Public declarations }
    Variavel: Pointer;
    fCaller: TObject;
    procedure Habilita;
    procedure Desabilita;
    procedure Retornar(resultado: TModalResult);
    property OnRetorno: TFuncaoRetorno read FOnRetorno write SetOnRetorno;
    property CallerControl: TControl read GetCallerControl write
      SetCallerControl;
  end;

var
  frmMdiChildPadrao: TfrmMdiChildPadrao;

implementation

{$R *.dfm}

procedure TfrmMdiChildPadrao.Desabilita;
begin
  Enabled := false;
end;

procedure TfrmMdiChildPadrao.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;

  if CallerControl <> nil then
  begin
    if CallerControl is TfrmMdiChildPadrao then
      (CallerControl as TfrmMdiChildPadrao).Habilita
    else
      CallerControl.Enabled := True;
  end;

  if Variavel <> nil then
    TForm(Variavel^) := nil;
end;

function TfrmMdiChildPadrao.GetCallerControl: TControl;
begin
  if fCaller is TControl then
    Result := TControl(fCaller)
  else
    Result := nil;
end;

procedure TfrmMdiChildPadrao.Habilita;
begin
  enabled := true;
end;

procedure TfrmMdiChildPadrao.Retornar(resultado: TModalResult);
begin
  if Assigned(FOnRetorno) then
    FOnRetorno(Self, Resultado)
  else
    Close;
end;

procedure TfrmMdiChildPadrao.SetCallerControl(const Value: TControl);
begin
  fCaller := Value;
end;

procedure TfrmMdiChildPadrao.SetOnRetorno(const Value: TFuncaoRetorno);
begin
  FOnRetorno := Value;
end;

procedure TfrmMdiChildPadrao.FormShow(Sender: TObject);
begin
  if CallerControl <> nil then
  begin
    if CallerControl is TfrmMdiChildPadrao then
      (CallerControl as TfrmMdiChildPadrao).Desabilita
    else
      CallerControl.Enabled := False;
  end;
end;

end.

